package gov.nist.applet.phone.ua.gui;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class ServerInfoXmlManager {
	Document doc;
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
	DocumentBuilder builder;
	NodeList numbers;
	String path;
	Map map = null;

	public NodeList getNumbers() {
		return numbers;
	}

	public void setNumbers(NodeList numbers) {
		this.numbers = numbers;
	}

	public ServerInfoXmlManager(String path) {
		super();
		this.path = path;
		// System.out.println(System.getProperty("user.dir"));
	}

	public ArrayList readXmlExtended() {
		ArrayList maps = new ArrayList();
		if (map == null) {
			map = new HashMap();
		}
		try {
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse(path);
			doc.normalize();
			Element root = doc.getDocumentElement();
			NodeList numbers = root.getChildNodes();
			this.setNumbers(numbers);
			for (int i = 0; i < numbers.getLength(); i++) {
				map = new HashMap();
				Node account = numbers.item(i);
				NodeList attr = account.getChildNodes();
				for (int j = 0; j < attr.getLength(); j++) {
					Element link = (Element) attr.item(j);
					map.put(link.getAttribute("value"), link.getTextContent());
				}
				maps.add(map);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return maps;
	}

	/**
	 * addCode
	 * 
	 * @param path
	 */
	public void addXmlCode(String nodename, String nodevalue) {
		try {
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse(path);
			doc.normalize();
			Text textseg;
			Element imag = doc.createElement("info");
			imag.setAttribute("value", nodename);
			textseg = doc.createTextNode(nodevalue);
			imag.appendChild(textseg);
			doc.getDocumentElement().appendChild(imag);
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer;
			transformer = tFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new java.io.File(path));
			transformer.transform(source, result);
		} catch (Exception e) {
		}
	}

	/**
	 * delete xml code
	 * 
	 * @param path
	 */
	public void delXmlCode() {
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(path);
			doc.normalize();
			NodeList imags = doc.getElementsByTagName("info");
			for (int i = 0; i < imags.getLength(); i++) {
				Element link = (Element) imags.item(i);
				doc.getFirstChild().removeChild(link);
			}
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new java.io.File(path));
			transformer.transform(source, result);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param path
	 *            should like this:file:\C:\a\b\c.xml
	 * @param result
	 */
	public boolean writerXML(String path, String result) {
		if (result == null)
			return false;
		result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><infos>"
				+ result + "</infos>";
		try {
			if (path.contains("file:")) {
				path = path.replace("%20", " ").substring(6);
			}
			BufferedWriter rw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(path)));
			rw.write(new String(result));
			rw.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
